<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends SB_Controller {

	function __construct()
	{
		parent::__construct();
	}
	
	function login()
	{
		if ($this->user->checkUser(trim(strtolower($this->input->post('wl'))), trim($this->input->post('wh'))))
		{
			$this->user->startSession($this->input->post('wl'));
			die('1');
		}
	}
	
	function logout()
	{
		$this->user->logout();
		if (isset($_GET['msie']))
			redirect('');
	}

	function updateMailruLinks()
	{
		die('lalla');
		// diePR($this->session->all_userdata());

		$res = $this->db->where(array(
			'pMethod' => 'mailru',
			'pLink' => '',
		))->get('user_auth')->result();

		if (!$res)
			die('not!');

		echo printr($res);

		$uids = array();
		foreach ($res as $user)
			$uids[]=$user->tUid;

		diePR($uids);

		$request_params = array(
			'app_id' => '707474',
			'method' => 'users.getInfo',
			'secure' => 1,
			'session_key' => $this->session->userdata('token'),
			'uids' => join(',', $uids),
		);
		
		$params = '';
		foreach ($request_params as $key => $value)
			$params .= "$key=$value";

		$url = 'http://www.appsmail.ru/platform/api'.
			'?'.http_build_query($request_params).
			'&sig='.md5($params.'c05861f1d415044b7178e222b368f27b');
		
		if (!($user = file_get_contents($url)))
			die('=(');

		$user_data = json_decode($user);

		foreach ($user_data as $user)
		{
			$this->db->where(array(
				'pMethod' => 'mailru',
				'tUid' => $user->uid
			))->update('user_auth', array('pLink' => $user->link));
		}

		diePR($user_data);
	}
}

?>